package cn.com.cifi.mars.service.impl;
import cn.com.cifi.mars.bean.bo.projecyByStages.FullPathOrgData;
import cn.com.cifi.mars.bean.dto.ProjectByStagesDto;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.HrOrgTypeEunm;
import cn.com.cifi.mars.entity.common.ProjectByStages;
import cn.com.cifi.mars.mapper.mars.ProjectByStagesMapper;
import cn.com.cifi.mars.service.ProjectByStagesService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 项目分期表 服务实现类
 * </p>
 *
 * @author JingYuanXiang
 * @since 2020-11-23
 */
@Service
public class ProjectByStagesServiceImpl extends ServiceImpl<ProjectByStagesMapper, ProjectByStages> implements ProjectByStagesService {


    @Autowired
    private ProjectByStagesMapper projectByStagesMapper;

    @Override
    public FullPathOrgData getParentFullPathOrgData(String orgId) {
        FullPathOrgData fullPathOrgData = new FullPathOrgData();

        ProjectByStagesDto projectByStages = projectByStagesMapper.selectByOrgdepid(orgId);
        if(projectByStages == null){
            return fullPathOrgData;
        }
        String orgnofullpath = projectByStages.getOrgnofullpath();
        List<String> orgIdList = Arrays.asList(orgnofullpath.split("-"));

        List<ProjectByStagesDto> projectByStagesList = projectByStagesMapper.selectByOrgdepids(orgIdList);
        if(CollectionUtils.isEmpty(projectByStagesList)){
            return fullPathOrgData;
        }
        for(ProjectByStagesDto org : projectByStagesList){
            String typeId = org.getHrOrgTypeId();
            if(typeId.equals(HrOrgTypeEunm.ORG_32.getKey())) {
                fullPathOrgData.setStageId(org.getOrgdepid());
                fullPathOrgData.setStageName(org.getOrgname());
            }else if(typeId.equals(HrOrgTypeEunm.ORG_31.getKey())) {
                fullPathOrgData.setProjectId(org.getOrgdepid());
                fullPathOrgData.setProjectName(org.getOrgname());
            }else if(typeId.equals(HrOrgTypeEunm.ORG_21.getKey())) {
                fullPathOrgData.setCityId(org.getOrgdepid());
                fullPathOrgData.setCityName(org.getOrgname());
            }else if(typeId.equals(HrOrgTypeEunm.ORG_20.getKey())) {
                fullPathOrgData.setRegionId(org.getOrgdepid());
                fullPathOrgData.setRegionName(org.getOrgname());
            }else if(typeId.equals(HrOrgTypeEunm.ORG_12.getKey())) {
                fullPathOrgData.setGroupId(org.getOrgdepid());
                fullPathOrgData.setGroupName(org.getOrgname());
            }
        }

        return fullPathOrgData;
    }

    @Override
    public List<FullPathOrgData> getFullPathOrgDataListList(){
        return projectByStagesMapper.selectFullPathOrgDataListList();
    }
}
